package com.hera.process.config.rest.delegate.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.hera.base.domain.WrapperJSON;
import com.hera.base.rest.delegate.BaseDelegate;
import com.hera.base.util.FiltroDTO;
import com.hera.process.config.domain.ParameterDetail;
import com.hera.process.config.rest.delegate.SecurityDelegate;
import com.hera.process.security.domain.SecGroupModule;
import com.hera.process.security.domain.SecurityGroup;
import com.hera.process.security.domain.User;

@Service
public class SecurityDelegateImpl extends BaseDelegate implements SecurityDelegate{

	@Autowired
	private RestTemplate restTemplate;
	
	@Value("${url.contexto.security}")
	private String urlController;
	
	@Value("${url.base}")
	private String urlBase;
	
	
	/*
	///////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////   SYSUSER	///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/


	@Override
	public List<User> findAllSysUsers(FiltroDTO filtroDTO) {
        
        return postForObject(urlBase+urlController+"sysuser/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<User>>>() {});

	}

	@Override
	public User createSysUser(User sysuser) {
		//return restTemplate.postForObject(urlBase+urlController+"sysuser/save", sysuser, User.class);
		return postForObject(urlBase+urlController+"sysuser/save", sysuser, 
        		new ParameterizedTypeReference<WrapperJSON<User>>() {});
		
	}

	@Override
	public User updatedSysUser(User sysuser) {
		
		//return restTemplate.postForObject(urlBase+urlController+"sysuser/update", sysuser, User.class);
		
		return postForObject(urlBase+urlController+"sysuser/update", sysuser, 
        		new ParameterizedTypeReference<WrapperJSON<User>>() {});
		
	}

	@Override
	public void deleteSysuser(User sysuser) {
		//restTemplate.postForObject(urlBase+urlController+"sysuser/delete", sysuser, Object.class);
		postForObject(urlBase+urlController+"sysuser/delete", sysuser, 
        		new ParameterizedTypeReference<WrapperJSON<User>>() {});
		
	}
	
	@Override
	public User validateUser(User sysuser) {
		//return restTemplate.postForObject(urlBase+urlController+"sysuser/validarUsuario", sysuser, User.class);
		
		return postForObject(urlBase+urlController+"sysuser/validarUsuario", sysuser, 
        		new ParameterizedTypeReference<WrapperJSON<User>>() {});
	}

	
	/*
	///////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////   SECURITY GROUP	///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/

	@Override
	public List<SecurityGroup> findAllSecurityGroups(FiltroDTO filtroDTO) {
	
        return postForObject(urlBase+urlController+"securitygroup/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<SecurityGroup>>>() {});
	}

	@Override
	public SecurityGroup createSecurityGroup(SecurityGroup secutritygroup) {
		
		return postForObject(urlBase+urlController+"securitygroup/save", secutritygroup, 
	        		new ParameterizedTypeReference<WrapperJSON<SecurityGroup>>() {});
	}

	@Override
	public SecurityGroup updatedSecurityGroup(SecurityGroup secutritygroup) {
		return postForObject(urlBase+urlController+"securitygroup/update", secutritygroup, 
        		new ParameterizedTypeReference<WrapperJSON<SecurityGroup>>() {});
	}

	@Override
	public void deleteSecurityGroup(SecurityGroup secutritygroup) {
		postForObject(urlBase+urlController+"securitygroup/delete", secutritygroup, 
        		new ParameterizedTypeReference<WrapperJSON<SecurityGroup>>() {});
	}

	/*
	///////////////////////////////////////////////////////////////////////////////////
	////////////////////////////////   SECGROUPMODULE	///////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////
	*/
	
	@Override
	public List<SecGroupModule> findAllSecGroupModules(FiltroDTO filtroDTO) {
        
        return postForObject(urlBase+urlController+"secgroupmodule/list", filtroDTO, 
        		new ParameterizedTypeReference<WrapperJSON<List<SecGroupModule>>>() {});
	}

	@Override
	public SecGroupModule createSecGroupModule(SecGroupModule scgmodule) {

		 return postForObject(urlBase+urlController+"secgroupmodule/save", scgmodule, 
	        		new ParameterizedTypeReference<WrapperJSON<SecGroupModule>>() {});
	}

	@Override
	public SecGroupModule updatedSecGroupModule(SecGroupModule scgmodule) {
		return postForObject(urlBase+urlController+"secgroupmodule/update", scgmodule, 
        		new ParameterizedTypeReference<WrapperJSON<SecGroupModule>>() {});
	}

	@Override
	public void deleteSecGroupModule(SecGroupModule scgmodule) {
		
		 postForObject(urlBase+urlController+"secgroupmodule/delete", scgmodule, 
        		new ParameterizedTypeReference<WrapperJSON<SecGroupModule>>() {});
	}

	@Override
	public List<ParameterDetail> findAllSecGroupModuleSiAsig(
			SecurityGroup securityGroup) {
		
        
        return postForObject(urlBase+urlController+"secgroupmoduleasigsi/list", securityGroup, 
        		new ParameterizedTypeReference<WrapperJSON<List<ParameterDetail>>>() {});
	}

	@Override
	public List<ParameterDetail> findAllSecGroupModuleNoAsig(
			SecurityGroup securityGroup) {
        
        return postForObject(urlBase+urlController+"secgroupmoduleasigno/list", securityGroup, 
        		new ParameterizedTypeReference<WrapperJSON<List<ParameterDetail>>>() {});
	}
}
